package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.ZLibUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * ${DESCRIPTION}
 *
 * @author yangcb
 * @email mudouyu@aliyun.com
 * @create 2018-03-24 10:56
 **/
@RestController
public class IndexController {

    @RequestMapping("login")
    public String login(String uname, String upwd) {
        if (null == uname) {
            uname = "";
        }
        if (null == upwd) {
            uname = "";
        }
        String str = "{'action':'login','data':{'uname':'" + uname + "','upwd':'" + upwd + "'}}";
        try {
            return ZLibUtils.huafei_enc(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return "error";
        }
    }

    @RequestMapping("get")
    public String get(String token, String carr, String prov, Integer face) {
        String str = "{'action':'get','token':" + token + ",'data':{'carr':'" + carr + "','prov':'" + prov + "', 'face':" + face + "}}";
        try {
            return ZLibUtils.huafei_enc(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return "error";
        }
    }


    @RequestMapping("dec")
    public String des(String body) {
        try {
            return ZLibUtils.huafei_dec(body, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return "error";
        }
    }


    @RequestMapping("getBill")
    public String getBill(String uname, String upwd, String carr, String prov, Integer face) {

        String str = "{'action':'login','data':{'uname':'" + uname + "','upwd':'" + upwd + "'}}";
        try {
            String enc = ZLibUtils.huafei_enc(str, "utf-8");
            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost("http://59.110.159.79:8085/api/request/bill.html");
            try {
                post.setHeader("application/x-www-form-urlencoded", enc);
                /*设置参数*/
                post.setEntity(new StringEntity(enc, "UTF-8"));
                HttpResponse response = client.execute(post);

                System.out.println("服务器响应码：" + response.getStatusLine().getStatusCode());

                HttpEntity entity = response.getEntity();
                String returnMsg = EntityUtils.toString(entity, "UTF-8");
                JSONObject jsonObject = JSON.parseObject(ZLibUtils.huafei_dec(returnMsg, "utf-8"));

                String token = jsonObject.getString("data");
                str = "{'action':'get','token':" + token + ",'data':{'carr':'" + carr + "','prov':'" + prov + "', 'face':" + face + "}}";
                enc = ZLibUtils.huafei_enc(str, "utf-8");
                client = HttpClients.createDefault();
                post = new HttpPost("http://59.110.159.79:8085/api/request/bill.html");

                post.setHeader("application/x-www-form-urlencoded", enc);
                /*设置参数*/
                post.setEntity(new StringEntity(enc, "UTF-8"));
                response = client.execute(post);
                System.out.println("服务器响应码：" + response.getStatusLine().getStatusCode());

                entity = response.getEntity();
                returnMsg = EntityUtils.toString(entity, "UTF-8");
                return ZLibUtils.huafei_dec(returnMsg, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                /*释放链接*/
                post.releaseConnection();
            }

        } catch (Exception e) {

        }
        return "";

    }


    @RequestMapping("bill")
    public String get(String body) {
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            body = new String(decoder.decode(body.getBytes()));
            String enc = ZLibUtils.huafei_enc(body, "utf-8");
            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost("http://59.110.159.79:8085/api/request/bill.html");
            try {
                post.setHeader("application/x-www-form-urlencoded", enc);
                /*设置参数*/
                post.setEntity(new StringEntity(enc, "UTF-8"));
                HttpResponse response = client.execute(post);
                System.out.println("服务器响应码：" + response.getStatusLine().getStatusCode());
                HttpEntity entity = response.getEntity();
                String returnMsg = EntityUtils.toString(entity, "UTF-8");
                return ZLibUtils.huafei_dec(returnMsg, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
                return "error";
            } finally {
                /*释放链接*/
                post.releaseConnection();
            }

        } catch (Exception e) {
            return "error";
        }


    }


}
